$(function(){
	$('a').bind('focus', function () { if (this.blur) { this.blur() }; }); // 去除a标签点击时的外部虚线
//	 $("#FullSearch").click(function () {
//        var keys = $("#sea_key").val()
//        if (keys == "项目/艺术家/作品" || keys == "") {
//            alert("查询关键词不能为空");
//            $("#sea_key").focus();
//            return;
//        }
//        keys = escape(keys);
//        //keys = unescape(keys);解码
//        window.location.href = "/Search.html?key=" + keys;
//    })
	
	if($("#show_menu").length>0)
	{
		$('#show_menu').click(function(){
			if($('.showmenu').attr("id")=='shown'){
				$('.showmenu').hide();
				$('.showmenu').attr("id","")
			}else{
				$('.showmenu').show();
				$('.showmenu').attr("id","shown")
			}
		}) 
	}
	if($("#shown").length==0)
	{
		$(".dh_cursor").hover( 
		  function(){ 
			//$('.showmenu').show();
			//$('.showmenu').attr("id","shown")
		  },
		  function(){
		  	$('.showmenu').hide();
			$('.showmenu').attr("id","")
		  }
		 );
	}
	if($(".item-show").length>0 && $(".i-mc").length>0)
	{
		$(".item-show >.item").hover( 
		  function(){ 
		  	$(this).addClass("selected");
		   	$(this).find('.i-mc').show();
		  },
		  function(){
		  	$(this).removeClass("selected");
		   	$(this).find('.i-mc').hide();
		  }
		 );
	}



$('#sea_key').focus(function () {
    if ($(this).val() == '') {
        $('#shelper').hide();
    } else {
        $('#shelper').show();
    }
}).blur(function () {
    setTimeout("$('#shelper').hide();", 500);
}).keyup(function (e) {
    var k = e.keyCode;
    if (k == 38 || k == 40) {
        return;
    } else if (k == 13) {
        $('#FullSearch').click(); return;
    }
    var v = $.trim($(this).val());
    $(this).val(v);
    if (v == '') { $('#shelper').hide(); return; }
    var ov = '';
    $('#shelper').show();
    if (ov == '' || v.substring(0, ov.length) != ov) {
        $('#shelper').html('<li>数据加载中...</li>');
        loadSearch(function () {

        }, v);
    } else {
        $('#shelper li').hide();
        var obj = $('#shelper li');
        if (v.match(/^([a-z]){1,4}$/)) {
            var s = v.substring(0, 1), m = '[name^="' + s + '"]'; m1 = ',[english^="' + s + '"]';
            for (var i = 1; i < v.length; i++) {
                s = v.substring(i, i + 1);
                m += '[name*="' + s + '"]'; m1 += '[english*="' + s + '"]';
            }
            obj = obj.filter(m + m1);
        } else {
            obj = obj.filter('[name^="' + v + '"],[english^="' + v + '"]');
        }
        if (obj.length == 0) {
            $('#no').show();
        } else {
            obj.show();
        }
    }
}).keydown(function (e) {
    var k = e.keyCode;
    if (k == 38 || k == 40) {
        var o = $('#shelper>li[v]:visible');
        if (o.length == 0) return;
        var obj;
        var s = o.filter('.dselected');
        if (s.length == 0) {
            obj = (k == 38 ? o.last() : o.first());
        } else {
            var j = o.index(s);
            if (k == 38) {
                obj = (j == 0 ? o.last() : $(o).get(j - 1));
            } else {
                obj = (j == o.length - 1 ? o.first() : $(o).get(j + 1));
            }
        }
        o.removeClass('dselected');
        $(obj).addClass('dselected');
        $('#shelper').scrollTop($(obj).offset().top - $('#shelper>li[v]:visible:first').offset().top);
    }
});
})


var SearchXml = [];
function loadSearch(f, value) {


    var path = $("#path").val();
    $.ajax({
        type: "POST",
        url: path+"/taskList/associativeSearch.html",
        data: {name:value},
        dataType: "json",
        success: function (data) {

            var html = "";

            html += " <li class=\"fore1\">";
            html += " <div  suggest-pos=\"0\" class=\"item1\" title=\""+value+"\"   onclick=\"window.location.href='"+path+"/artistList/artistList.html?keyword="+encodeURI(encodeURI(value))+"'\">";
            html += "     <div class=\"search-item\">在<strong>艺术家</strong>中搜索<strong>"+value+"</strong></div>";
            html += "  </div>";
            html += " <div  suggest-pos=\"0\" class=\"item1\" title=\""+value+"\"   onclick=\"window.location.href='"+path+"/productionList/productionList.html?keyword="+encodeURI(encodeURI(value))+"'\">";
            html += "     <div class=\"search-item\">在<strong>作品</strong>中搜索<strong>"+value+"</strong></div>";
            html += "  </div>";

            //html += " <a href='${path}/artistList/artistList.do?keyword="+value+"'><li>在艺术家中搜索“"+value+"”</li></a>";
            //html += " <a href='${path}/productionList/productionList.do?keyword="+value+"'><li>在作品中搜索“"+value+"”</li></a>";
            html += " </li>";

            for(var i in data){

                var bean = data[i];

                //html += " <a href='${path}/taskdetail/index.do?taskId="+bean.item_id+"'><li>"+bean.title+"</li></a>";

                html += " <li   title='"+value+"' onclick=\"window.location.href='"+path+"/taskdetail/index.html?taskId="+bean.item_id+"'\">";
                html += "     <div class=\"search-item\">"+bean.title+"</div>";

                html += " </li>";

            }




            html += " <li class=\"close\" >关闭</li>";



            $("#shelper").html(html);
        }
    });

    //这里是获取数据的地方
    /*if (SearchXml[t] == undefined) {
     $.ajax({
     url:'?key=' + t,
     cache: false,
     async: false,
     dataType: 'xml',
     success: function (xml) {
     SearchXml[t] = $(xml).find('row');
     SearchXml[t].each(function () {
     $(this).attr({ t_name: $(this).find('t_name').text().toLowerCase(), t_count: $(this).find('t_count').text().toLowerCase() });
     });

     }
     });
     }*/
    f();
}

function clickItem(obj)
{
    var keys = $(obj).attr("title");
    keys = escape(keys);
    //keys = unescape(keys);解码
    window.location.href = "/Search.html?key=" + keys;
}